#ifndef FILELISTINDEX_H
#define FILELISTINDEX_H

#include <QList>
#include <QDebug>

/**
 * FileListIndex - 文件列表索引管理器
 *
 * Martin Fowler重构：解决 Primitive Obsession 代码异味
 * 将原始int索引封装成类型安全的索引管理器，提供边界检查和验证
 */
class FileListIndex
{
public:
    // 构造函数
    explicit FileListIndex(int value = -1) : m_value(value) {}

    // 静态工厂方法 - 提供语义清晰的创建方式
    static FileListIndex invalid() { return FileListIndex(-1); }
    static FileListIndex first() { return FileListIndex(0); }
    static FileListIndex at(int position) { return FileListIndex(position); }
    static FileListIndex last(int listSize) {
        return listSize > 0 ? FileListIndex(listSize - 1) : invalid();
    }
    static FileListIndex afterLast(int listSize) { return FileListIndex(listSize); }

    // 验证方法
    bool isValid() const { return m_value >= 0; }
    bool isValidFor(int listSize) const {
        return m_value >= 0 && m_value < listSize;
    }
    bool isValidInsertPositionFor(int listSize) const {
        return m_value >= 0 && m_value <= listSize;
    }

    // 访问器
    int value() const { return m_value; }
    int safeValue(int listSize) const {
        if (!isValid()) return -1;
        return qBound(0, m_value, listSize - 1);
    }
    int safeInsertPosition(int listSize) const {
        if (!isValid()) return listSize;
        return qBound(0, m_value, listSize);
    }

    // 算术操作 - 返回新的索引对象
    FileListIndex next() const { return FileListIndex(m_value + 1); }
    FileListIndex previous() const { return FileListIndex(qMax(-1, m_value - 1)); }
    FileListIndex offset(int delta) const { return FileListIndex(m_value + delta); }

    // 比较操作
    bool operator==(const FileListIndex& other) const { return m_value == other.m_value; }
    bool operator!=(const FileListIndex& other) const { return m_value != other.m_value; }
    bool operator<(const FileListIndex& other) const { return m_value < other.m_value; }
    bool operator>(const FileListIndex& other) const { return m_value > other.m_value; }

    // 调试输出
    QString toString() const {
        return isValid() ? QString("Index(%1)").arg(m_value) : "Index(invalid)";
    }

private:
    int m_value;
};

/**
 * FileListIndexRange - 索引范围管理器
 *
 * 用于处理批量操作，如多选拖拽等
 */
class FileListIndexRange
{
public:
    explicit FileListIndexRange(const QList<int>& indices = {}) {
        for (int idx : indices) {
            if (idx >= 0) {
                m_indices.append(FileListIndex::at(idx));
            }
        }
        sortIndices();
    }

    explicit FileListIndexRange(const QList<FileListIndex>& indices)
        : m_indices(indices) {
        sortIndices();
    }

    // 验证方法
    bool isEmpty() const { return m_indices.isEmpty(); }
    bool allValidFor(int listSize) const {
        for (const auto& index : m_indices) {
            if (!index.isValidFor(listSize)) return false;
        }
        return true;
    }

    // 访问器
    int count() const { return m_indices.size(); }
    int size() const { return m_indices.size(); }
    QList<FileListIndex> indices() const { return m_indices; }
    QList<int> rawIndices() const {
        QList<int> result;
        for (const auto& index : m_indices) {
            result.append(index.value());
        }
        return result;
    }

    // 获取安全的索引列表
    QList<int> safeIndices(int listSize) const {
        QList<int> result;
        for (const auto& index : m_indices) {
            if (index.isValidFor(listSize)) {
                result.append(index.value());
            }
        }
        return result;
    }

    // 迭代器支持
    using iterator = QList<FileListIndex>::iterator;
    using const_iterator = QList<FileListIndex>::const_iterator;
    
    iterator begin() { return m_indices.begin(); }
    iterator end() { return m_indices.end(); }
    const_iterator begin() const { return m_indices.begin(); }
    const_iterator end() const { return m_indices.end(); }
    const_iterator cbegin() const { return m_indices.cbegin(); }
    const_iterator cend() const { return m_indices.cend(); }

    // 调试输出
    QString toString() const {
        QStringList parts;
        for (const auto& index : m_indices) {
            parts.append(index.toString());
        }
        return QString("Range[%1]").arg(parts.join(", "));
    }

private:
    void sortIndices() {
        std::sort(m_indices.begin(), m_indices.end());
    }

    QList<FileListIndex> m_indices;
};

// 调试输出支持
inline QDebug operator<<(QDebug debug, const FileListIndex& index)
{
    debug.nospace() << index.toString();
    return debug;
}

inline QDebug operator<<(QDebug debug, const FileListIndexRange& range)
{
    debug.nospace() << range.toString();
    return debug;
}

#endif // FILELISTINDEX_H